package com.firefighting.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.firefighting.entity.MaintenancePlan;
import com.firefighting.entity.MaintenanceplanContent;
import com.firefighting.entity.Task;
import com.firefighting.entity.TaskItem;
@Service 
public class InsertUpdateMaintenanceService {

	@Autowired
	private MaintenancePlanService maintenanncePlanService;
	@Autowired
	private ContractService contractService;
	@Autowired
	private TaskService taskService;
	@Autowired
	private TaskItemService taskItemService;
	@Autowired
	private MaintenanceplanContentService maintenanceplanContentService;

	/*Hanjd 下发内容*/

	@Transactional(rollbackFor = Exception.class)
	public void InsertUpdateMaintenance(Task task,MaintenancePlan maintenancePlan, JSONArray jsonArray,Integer planId,Integer buildingId)
	{
		taskService.insertSelective(task);
		maintenanncePlanService.updateMaintenanceplan(maintenancePlan);

		JSONObject jsonOne = null;
		
		//插入taskItem数据，更新maintenancePlan_content里面的下发状态
		for (int i = 0; i < jsonArray.size(); i++) {
			jsonOne = jsonArray.getJSONObject(i);
			TaskItem taskItem = new TaskItem();
			System.out.println("planId---"+planId);
			System.out.println("ContentId---"+Integer.valueOf(jsonOne.getString("id")));
			MaintenanceplanContent maintenanceplanContent = maintenanceplanContentService.queryByPlanIdContentId(planId, Integer.valueOf(jsonOne.getString("id")),buildingId);
			maintenanceplanContent.setDevelopment("已下发");
			taskItem.setMaintenanceDetailId(Integer.valueOf(jsonOne.getString("id")));
			taskItem.setTaskId(task.getId());
			taskItem.setExecuteTime(jsonOne.getDate("date"));
			System.out.println("---taskItem1---"+taskItem.getMaintenanceDetailId());
			System.out.println("---taskItem2---"+taskItem.getTaskId());
			System.out.println("---taskItem3---"+taskItem.getExecuteTime());
			taskItemService.insertSelective(taskItem);
			maintenanceplanContentService.updateMaintenanceplanContent(maintenanceplanContent);
			
		}
	}
}
